package com.vend.common.base
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.vend.common.base.model.FragmentType
import com.vend.common.mvp.IPresenter
import org.greenrobot.eventbus.EventBus

/**
 * Created by glendal on 2022/01/17.
 * 创建基层类BaseFragment
 */
abstract class BaseFragment<T : IPresenter> : Fragment() {
    //顶级的绑定布局的View
    var v :View?= null
    //presenter
    var mPresenter : T ?= null
    //上下文
    lateinit var mSelf :Context
    //创建一个类型，为后续ARouter跳转加载Fragment的时候的逻辑处理
    var mFragmentType = FragmentType.Fragnemt_Normal.fType
    //上下文的获取
    override fun onAttach(context: Context) {
        super.onAttach(context)
        mSelf = context
    }
    //设置布局
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        if (null == v) {
            v = inflater.inflate(getContentViewId(), container, false)
        }
        return v
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView(view,savedInstanceState)
    }

    /**
     * 获取FragmentType类型
     */
    open fun getFragmentType():Int{
        return mFragmentType
    }
    fun setFragmentType(type : Int){
        this.mFragmentType = type
    }

    /**
     * 在这里进行布局的设置
     * kotlin-android-extensions
     * 插件在Fragment 中的使用
     */
    abstract fun initView(view: View, savedInstanceState: Bundle?)
    /**
     * 设置布局加载
     */
    abstract fun getContentViewId():Int

    /**
     * 是否使用EventBus 事件总线
     * 可以在子类进行重写的处理
     */
    open fun useEventBus():Boolean{
        return false
    }
    override fun onStart() {
        super.onStart()
        if (!useEventBus()){
            return
        }
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }
    }
    override fun onDestroyView() {
        super.onDestroyView()
        if (!useEventBus()){
            return
        }
        //注销EventBus
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this)
        }
        isLoaded = false
        isVisibleToUser = false
        isCallResume = false
    }

    /**
     * 是否执行懒加载
     */
    private var isLoaded = false

    /**
     * 当前Fragment是否对用户可见
     */
    private var isVisibleToUser = false

    /**
     * 当使用ViewPager+Fragment形式会调用该方法时，setUserVisibleHint会优先Fragment生命周期函数调用，
     * 所以这个时候就,会导致在setUserVisibleHint方法执行时就执行了懒加载，
     * 而不是在onResume方法实际调用的时候执行懒加载。所以需要这个变量
     */
    private var isCallResume = false

    override fun onResume() {
        super.onResume()
        isCallResume = true
        judgeLazyInit()
    }


    private fun judgeLazyInit() {
        if (isVisibleToUser) {
            if (!isLoaded && isVisibleToUser && isCallResume) {
                lazyInit()
                isLoaded = true
            }
        }else{
            if (!isLoaded &&  isCallResume) {
                lazyInit()
                isLoaded = true
            }
        }
    }
    var isViewPage = true
    fun isViewPageFragment():Boolean{
        return isViewPage
    }

    /**
     * 设置为ViewPage
     */
    fun setViewPageFragment(isViewPage : Boolean){
        this.isViewPage = isViewPage
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        isVisibleToUser = !hidden
        judgeLazyInit()
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        this.isVisibleToUser = isVisibleToUser
        judgeLazyInit()
    }

    abstract fun lazyInit()
}